<?php

class SmsadminController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $sessionAdmin = new Zend_Session_Namespace('Zend_Auth_Admin');
        $this->sessAdmin = $sessionAdmin->data;

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        $this->_helper->layout->setLayout('smsadmin');
        

        if(!isset($this->sessAdmin)){

            $param = array(
            'action' => 'admin',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );

            
            $this->_redirect($this->view->url($param, 'default', true));
        }

        $this->sms = New Application_Model_Sms();
        
        

    }

    public function indexAction()
    {
        //
    }

    public function broadcastAction()
    {


        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            //Member Detail
            $tujuan  = str_replace(";",",",$formData['tujuan']);
            $pesan = $formData['pesan'];

            
            
            if ($tujuan == ""){  //Kirim ke all mamber

                //
                $member = new Application_Model_Member();
                $members = $member->getMemberContact("contact_type_id", "1");

                for ($x=0; $x<count($members); $x++){
                    $this->sms->sendSMS("NIK7777777", $members[$x]['mobile_no'], $pesan, "0");
                }

                


            }else{  //Kirim ke nomor yg diinput
                $arrTlp = explode(",", $tujuan);

                for ($x=0; $x<count($arrTlp); $x++){

                    $tlp = trim($arrTlp[$x]);

                    if ($tlp != ""){
                        // Insert Outbox
                        $this->sms->sendSMS("NIK7777777", $tlp, $pesan, "0");
                        
                    }
                }
            }

            
            /*
            $this->view->code = $code;

            if($code == 0){
                $param = array(
                'action' => 'sendsms',
                'controller' => 'admin',
                'code' => $code,
                'msg' => $msg
                );

                $this->_redirect($this->view->url($param, 'default', true));
            }else{
                $this->view->errorMsg = $msg;
                $this->view->data = $this->param;
            }
             * 
             */

            $this->view->code = '0';
            $this->view->msg = 'Broadcast SMS Sukses';


        }
        
    }


    public function sisapulsaAction(){

        // new HTTP request to some HTTP address
        $client = new Zend_Http_Client('http://api.keajaibancbr.com/index/saldoppob');
        // set some parameters

        // POST request
        $response = $client->request(Zend_Http_Client::POST);


        $resp = json_decode($response->getBody());

        $saldoPPOB = $resp->saldo;


        // new HTTP request to some HTTP address
        $client2 = new Zend_Http_Client('http://api.keajaibancbr.com/index/saldoreply');
        // set some parameters

        // POST request
        $response2 = $client2->request(Zend_Http_Client::POST);


        $resp2 = json_decode($response2->getBody());

        $saldoReply = explode('saldo=', $resp2->msg);


        $this->view->ppob = number_format($saldoPPOB);
        $this->view->reply = number_format($saldoReply[1]);

    }
}

